﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using EasyHome.Models;
using EasyHome.DAL;

namespace EasyHome.Controllers
{
    public class ContactoDatoController : Controller
    {
        private EasyHomeContext db = new EasyHomeContext();

        // GET: /ContactoDato/
        public ActionResult Index()
        {
            var datoscontacto = db.DatosContacto.Include(c => c.Contacto).Include(c => c.TablaDato);
            return View(datoscontacto.ToList());
        }

        // GET: /ContactoDato/Details/5
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ContactoDato contactodato = db.DatosContacto.Find(id);
            if (contactodato == null)
            {
                return HttpNotFound();
            }
            return View(contactodato);
        }

        // GET: /ContactoDato/Create
        public ActionResult Create()
        {
            ViewBag.ContactoId = new SelectList(db.Contactos, "Id", "Nombre");
            ViewBag.TipoId = new SelectList(db.TablasDatos, "id", "Descripcion");
            return View();
        }

        // POST: /ContactoDato/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="id,Dato,TipoId,ContactoId")] ContactoDato contactodato)
        {
            if (ModelState.IsValid)
            {
                contactodato.id = Guid.NewGuid();
                db.DatosContacto.Add(contactodato);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ContactoId = new SelectList(db.Contactos, "Id", "Nombre", contactodato.ContactoId);
            ViewBag.TipoId = new SelectList(db.TablasDatos, "id", "Descripcion", contactodato.TipoId);
            return View(contactodato);
        }


        // GET: /ContactoDato/Edit/5
        public  PartialViewResult Edit(Guid? id)
        {
            if (id == null)
            {
                //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ContactoDato contactodato = db.DatosContacto.Find(id);
            if (contactodato == null)
            {
                //return HttpNotFound();
            }
            ViewBag.ContactoId = new SelectList(db.Contactos, "Id", "Nombre", contactodato.ContactoId);
            ViewBag.TipoId = new SelectList(db.TablasDatos, "id", "Descripcion", contactodato.TipoId);
            return PartialView(contactodato);
        }

        // POST: /ContactoDato/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="id,Dato,TipoId,ContactoId")] ContactoDato contactodato)
        {
            if (ModelState.IsValid)
            {
                db.Entry(contactodato).State = EntityState.Modified;
                db.SaveChanges();
                
                Contacto contacto = db.Contactos.Find(contactodato.ContactoId);

                return View("~/Views/Contacto/Edit.cshtml", contacto);
            }
            ViewBag.ContactoId = new SelectList(db.Contactos, "Id", "Nombre", contactodato.ContactoId);
            ViewBag.TipoId = new SelectList(db.TablasDatos, "id", "Descripcion", contactodato.TipoId);
            
            return View("~/Views/Contacto/Edit.cshtml", contactodato.Contacto);
        }

        // GET: /ContactoDato/Delete/5
        public ActionResult Delete(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ContactoDato contactodato = db.DatosContacto.Find(id);
            if (contactodato == null)
            {
                return HttpNotFound();
            }
            return View(contactodato);
        }

        // POST: /ContactoDato/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Guid id)
        {
            ContactoDato contactodato = db.DatosContacto.Find(id);
            db.DatosContacto.Remove(contactodato);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
